package com.hushui.mall.commodity.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hushui.mall.commodity.entity.ProductAttrValueEntity;
import com.hushui.mall.commodity.service.ProductAttrValueService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * spu属性值
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:45
 */
@RestController
public class ProductAttrValueController {
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("commodity/productattrvalue/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productAttrValueService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/product/attr/update/{spuId}")
    public R modifyProduct(@PathVariable(value = "spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> entity){
        ArrayList<ProductAttrValueEntity> entities = new ArrayList<>();
        for (ProductAttrValueEntity a:entity
             ) {
            QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("spu_id",spuId).eq("attr_id",a.getAttrId());
            ProductAttrValueEntity one = productAttrValueService.getOne(wrapper);
            one.setAttrName(a.getAttrName());
            one.setAttrValue(a.getAttrValue());
            one.setQuickShow(a.getQuickShow());
            entities.add(one);
        }
        productAttrValueService.updateBatchById(entities);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("commodity/productattrvalue/info/{id}")
    public R info(@PathVariable("id") Long id){
		ProductAttrValueEntity productAttrValue = productAttrValueService.getById(id);

        return R.ok().put("productAttrValue", productAttrValue);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/productattrvalue/save")
    public R save(@RequestBody ProductAttrValueEntity productAttrValue){
		productAttrValueService.save(productAttrValue);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/productattrvalue/update")
    public R update(@RequestBody ProductAttrValueEntity productAttrValue){
		productAttrValueService.updateById(productAttrValue);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("commodity/productattrvalue/delete")
    public R delete(@RequestBody Long[] ids){
		productAttrValueService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
